package com.sykj.bsc.zjj.controller;

import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bsc.util.JsonUtils;
import com.sykj.bsc.zjj.bean.Bis_business_order;
import com.sykj.bsc.zjj.bean.Bis_custom_user;
import com.sykj.bsc.zjj.bean.Bis_shop;
import com.sykj.bsc.zjj.service.Bis_custom_userService;

@Controller
@RequestMapping("zjj")
public class Bis_custom_userController {
	@Autowired
	private Bis_custom_userService bcus;
	
	/**
	 * 查询
	 * 主表
	 * Bis_custom_user
	 * 顾客用户信息表
	 * @param map
	 * @param bis_custom_user
	 * @return
	 */
	@RequestMapping("selectBis_custom_user")
	public String selectBis_custom_user(Map<String, Object> map,Model model,Bis_custom_user bis_custom_user,@RequestParam(value = "page", defaultValue = "1") Integer page) {
		PageHelper.startPage(page,5);
		
		//定值
		if(bis_custom_user!=null) getBis_custom_user1(model, bis_custom_user);
		
		/**
		 * 查询所有
		 */
		List<Map> list = this.bcus.selectBis_custom_user(bis_custom_user);
		/*map.put("list", list);*/
		PageInfo<Map> info = new PageInfo<>(list);
		map.put("info", info);
		/**
		 * 查询
		 * 店铺信息表
		 */
		List<Bis_shop> list2 = this.bcus.selectBis_shop();
		map.put("list2", list2);
		return "backstage/zjj/customerselect";
	}
	
	/**
	 * 条件查询定值方法
	 * @param model
	 * @param bcu
	 */
	private void getBis_custom_user1(Model model,Bis_custom_user bcu) {
		if (bcu.getMobile()!=null && !bcu.getMobile().equals("")) {
			model.addAttribute("mobile", bcu.getMobile());
		}
		if (bcu.getUsername()!=null && !bcu.getUsername().equals("")) {
			model.addAttribute("username", bcu.getUsername());
		}
		if (bcu.getShop_id()!=null && !bcu.getShop_id().equals("")) {
			model.addAttribute("shop_id", bcu.getShop_id());
		}
		if (bcu.getEnroll_mode()!=null && !bcu.getEnroll_mode().equals("")) {
			model.addAttribute("enroll_mode", bcu.getEnroll_mode());
		}
		if (bcu.getCustom_user_id()!=null && !bcu.getCustom_user_id().equals("")) {
			model.addAttribute("custom_user_id", bcu.getCustom_user_id());
		}
		if (bcu.getNikename()!=null && !bcu.getNikename().equals("")) {
			model.addAttribute("nikename", bcu.getNikename());
		}
		if (bcu.getWechat_status()!=null && !bcu.getWechat_status().equals("")) {
			model.addAttribute("wechat_status", bcu.getWechat_status());
		}
		if (bcu.getRecent_time()!=null && !bcu.getRecent_time().equals("")) {
			model.addAttribute("recent_time", bcu.getRecent_time());
		}
	}
	
	/**
	 * 新增
	 * Bis_custom_user
	 * 顾客用户信息表
	 * @return
	 */
	@RequestMapping(value="/insertBis_custom_user",method = RequestMethod.POST)
	public String insertBis_custom_user(Bis_custom_user bis_custom_user,@RequestParam("pic") MultipartFile file ,HttpServletRequest request) {
	/*@RequestMapping("insertBis_custom_user")
	public String insertBis_custom_user(Bis_custom_user bis_custom_user) {	*/
	/**
		 * 生成随机用户编号
		 */
		SimpleDateFormat sdf1=new SimpleDateFormat("yyMMddHHmmss");
		String str1=sdf1.format(new Date());
		Random random1=new Random();
		int num1= random1.nextInt(9999)+1000;
		String post_id=str1+num1;

		//上传
		String path = request.getRealPath("/");
		File file1 = new File(path);
		/*path=file1.getParent(); */
		path = path + "/upload/";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		path = path + sdf.format(date);
		File dir = new File(path);
		if (!dir.exists()) {
			// 如果该目录不存在则创建目录
			dir.mkdirs();
		}
		File newfile = new File(path, file.getOriginalFilename());
		try {
			if (!newfile.exists()) {
				// 此时创建之后只是一个空的文件
				newfile.createNewFile();
			}
			// 将file中的内容复制到newfile中
			file.transferTo(newfile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		bis_custom_user.setPhoto("/upload/" + sdf.format(date) + "/" + file.getOriginalFilename());
		
		bis_custom_user.setCustom_user_id(post_id);
		this.bcus.insertBis_custom_user(bis_custom_user);
		return "redirect:/zjj/selectBis_custom_user";		
	}
	
	/**
	 * 根据ID查询
	 * Bis_custom_user
	 * 顾客用户信息表
	 * @return
	 */
	@RequestMapping("findByIdBis_custom_user")
	public void findByIdBis_custom_user(String custom_user_id,HttpServletResponse response) {
		this.getWirte(response, this.bcus.findByIdBis_custom_user(custom_user_id));
	}
	public void getWirte(HttpServletResponse response,Object data) {
		try {
			String json=JsonUtils.objectToJson(data);
			PrintWriter	out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改
	 * Bis_custom_user
	 * 顾客用户信息表
	 * @return
	 */
	@RequestMapping(value="/updateBis_custom_user",method = RequestMethod.POST)
	public String updateBis_custom_user(Bis_custom_user bis_custom_user,@RequestParam("file") MultipartFile file ,HttpServletRequest request) {
		// 上传
		String path=request.getRealPath("/");//tomcat/project
		File file1=new File(path);
		//path=file1.getParent();//webapp
		path=path+"/upload/";
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		//path:c:/tools/tomcat/webapp/upload/20180719
		path=path+sdf.format(date);
		File dir=new File(path);
		if(!dir.exists()){
			//如果该目录不存在则创建目录
			dir.mkdirs();
		}
		File photo=new File(path,file.getOriginalFilename());
		try{
			if(!photo.exists()){
				//此时创建之后只是一个空的文件
				photo.createNewFile();
			}
			//将file中的内容复制到newfile中
			file.transferTo(photo);
		}catch(Exception e){
			e.printStackTrace();
		}
		String a=file.getOriginalFilename();
		if(a!=null &&!a.equals("") ){
			bis_custom_user.setPhoto("/upload/"+sdf.format(date)+"/"+file.getOriginalFilename());
		}
		
		this.bcus.updateBis_custom_user(bis_custom_user);
		return "redirect:/zjj/selectBis_custom_user";		
	}
	
	/**
	 * 删除
	 * Bis_custom_user
	 * 顾客用户信息表
	 * @return
	 */
	@RequestMapping("deleteBis_custom_user")
	public String deleteBis_custom_user(String custom_user_id) {
		this.bcus.deleteBis_custom_user(custom_user_id);
		return "redirect:/zjj/selectBis_custom_user";		
	}
	
	/**
	 * 根据ID查询
	 * Bis_custom_user
	 * @param custom_user_id
	 * @param map
	 * @return
	 */
	@RequestMapping("findByIdBis_custom_user1/{custom_user_id}")
	public String findByIdBis_custom_user1(@PathVariable("custom_user_id") String custom_user_id,Map<String, Object> map) {
		Bis_custom_user bis_custom_user =  this.bcus.findByIdBis_custom_user(custom_user_id);
		map.put("bis_custom_user", bis_custom_user);
		return "backstage/zjj/customerViewthedetails";
	}
	
	/**
	 * 根据ID查询
	 * Bis_custom_user
	 * @param custom_user_id
	 * @param map
	 * @return
	 */
	@RequestMapping("findByIdBis_custom_user2/{custom_user_id}")
	public String findByIdBis_custom_user2(@PathVariable("custom_user_id") String custom_user_id,Map<String, Object> map) {
		Bis_custom_user bis_custom_user =  this.bcus.findByIdBis_custom_user(custom_user_id);
		map.put("bis_custom_user", bis_custom_user);
		return "backstage/zjj/customerBasicdetails";
	}
	
	@RequestMapping("selectBis_business_order")
	public String selectBis_business_order(@RequestParam("custom_user_id") String custom_user_id,Model model,Bis_business_order bbo,Map<String, Object> map,@RequestParam(value = "page", defaultValue = "1") Integer page) {
		//定值
		if(bbo!=null) getBis_business_order1(model, bbo);
		
		bbo.setCustom_user_id(custom_user_id);
		List<Map> list = this.bcus.selectBis_business_order(bbo);
		map.put("custom_user_id", custom_user_id);
		//map.put("list", list);
		PageHelper.startPage(page,5);
		PageInfo<Map> info = new PageInfo<>(list);
		map.put("info", info);
		/**
		 * 查询
		 * 店铺信息表
		 */
		List<Bis_shop> list2 = this.bcus.selectBis_shop();
		map.put("list2", list2);
		return "backstage/zjj/customerConsumptionsituation";
	}
	
	/**
	 * 条件查询定值方法
	 * @param model
	 * @param bbo
	 */
	private void getBis_business_order1(Model model,Bis_business_order bbo) {
		if (bbo.getOrder_id()!=null && !bbo.getOrder_id().equals("")) {
			model.addAttribute("order_id", bbo.getOrder_id());
		}
		if (bbo.getOrder_content()!=null && !bbo.getOrder_content().equals("")) {
			model.addAttribute("order_content", bbo.getOrder_content());
		}
		if (bbo.getPay_way()!=null && !bbo.getPay_way().equals("")) {
			model.addAttribute("pay_way", bbo.getPay_way());
		}
		if (bbo.getOrder_status()!=null && !bbo.getOrder_status().equals("")) {
			model.addAttribute("order_status", bbo.getOrder_status());
		}
		if (bbo.getShop_id()!=null && !bbo.getShop_id().equals("")) {
			model.addAttribute("shop_id", bbo.getShop_id());
		}
		if (bbo.getServer_user_id()!=null && !bbo.getServer_user_id().equals("")) {
			model.addAttribute("server_user_id", bbo.getServer_user_id());
		}
		if (bbo.getHandle_user_id()!=null && !bbo.getHandle_user_id().equals("")) {
			model.addAttribute("handle_user_id", bbo.getHandle_user_id());
		}
		if (bbo.getFinish_time1()!=null && !bbo.getFinish_time1().equals("")) {
			model.addAttribute("finish_time1", bbo.getFinish_time1());
		}
		if (bbo.getFinish_time2()!=null && !bbo.getFinish_time2().equals("")) {
			model.addAttribute("finish_time2", bbo.getFinish_time2());
		}
	}
	
	/**
	 * Excel导出
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/export")
	@ResponseBody
	public void report(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String arr=request.getParameter("arr");
		arr=arr.substring(0, arr.length()-1);
		String[] str=arr.split("&");
		String[]title=str[0].split(",");
		String fileName = "顾客消费信息表"+System.currentTimeMillis()+".xls";
		String sheetName = "顾客消费信息表";	
		String[][] content=new String[str.length][title.length];
		for(int i=0;i<str.length-1;i++){
			content[i]=str[i+1].split(",");
		}
		HSSFWorkbook wb = ExportUtils.getHSSFWorkbook(sheetName, title, content, null);
		try {
			this.setResponseHeader(response, fileName);
			OutputStream os = response.getOutputStream();
			wb.write(os);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
	
	/**
	 * 顾客列表
	 * 未服务项目
	 * @param custom_user_id
	 * @param model
	 * @param bbo
	 * @param map
	 * @param page
	 * @return
	 */
	@RequestMapping("selectBis_business_order2")
	public String selectBis_business_order2(@RequestParam("custom_user_id") String custom_user_id,Model model,Bis_business_order bbo,Map<String, Object> map,@RequestParam(value = "page", defaultValue = "1") Integer page) {
		//定值
		if(bbo!=null) getBis_business_order2(model, bbo);
		
		bbo.setCustom_user_id(custom_user_id);
		List<Map> list = this.bcus.selectBis_business_order2(bbo);
		map.put("custom_user_id", custom_user_id);
		//map.put("list", list);
		PageHelper.startPage(page,5);
		PageInfo<Map> info = new PageInfo<>(list);
		map.put("info", info);
		/**
		 * 查询
		 * 店铺信息表
		 */
		List<Bis_shop> list2 = this.bcus.selectBis_shop();
		map.put("list2", list2);
		return "backstage/zjj/customerNonserviceitems";
	}
	
	/**
	 * 条件查询定值方法
	 * @param model
	 * @param bbo
	 */
	private void getBis_business_order2(Model model,Bis_business_order bbo) {
		if (bbo.getOrder_id()!=null && !bbo.getOrder_id().equals("")) {
			model.addAttribute("order_id", bbo.getOrder_id());
		}
		if (bbo.getOrder_content()!=null && !bbo.getOrder_content().equals("")) {
			model.addAttribute("order_content", bbo.getOrder_content());
		}
		if (bbo.getShop_id()!=null && !bbo.getShop_id().equals("")) {
			model.addAttribute("shop_id", bbo.getShop_id());
		}
		if (bbo.getSubscribe_time1()!=null && !bbo.getSubscribe_time1().equals("")) {
			model.addAttribute("subscribe_time1", bbo.getSubscribe_time1());
		}
		if (bbo.getSubscribe_time2()!=null && !bbo.getSubscribe_time2().equals("")) {
			model.addAttribute("subscribe_time2", bbo.getSubscribe_time2());
		}
	}
	
	/**
	 * 查询
	 * 根据编号查询订单详情
	 * Bis_business_order
	 * @param custom_user_id
	 * @param response
	 */
	@RequestMapping("selectBis_business_order1/{order_id}")
	public String selectBis_business_order1(@PathVariable("order_id") String order_id,Map<String, Object> map) {
		Bis_business_order bso = this.bcus.selectBis_business_order1(order_id);
		map.put("bso", bso);
		return "backstage/zjj/customerConsumptionsituation";
	}
	
	/**
	 * 查询顾客用户信息表
	 * 判断手机是否重复
	 * @param mobile
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_custom_usermobile")
	public void findByIdBis_custom_usermobile(Bis_custom_user mobile,HttpServletRequest request,HttpServletResponse response) {
		String mobile1=request.getParameter("mobile");
		Integer mobile11 = this.bcus.findByIdBis_custom_usermobile(mobile1);
		boolean b1;
		if (mobile11==0) {
			b1 = true;
		} else {
			b1 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b1);
		resjson(response, map);
	}
	private void resjson(HttpServletResponse response,Object obj){
    	ObjectMapper  om=new ObjectMapper();
    	try {
			String json=om.writeValueAsString(obj);
			PrintWriter out=response.getWriter();
			out.write(json);
			out.flush();
			out.close();
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
	
	/**
	 * 查询顾客用户信息表
	 * 判断电话是否重复
	 * @param phone
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_custom_userphone")
	public void findByIdBis_custom_userphone(Bis_custom_user phone,HttpServletRequest request,HttpServletResponse response) {
		String phone1=request.getParameter("phone");
		Integer phone11 = this.bcus.findByIdBis_custom_userphone(phone1);
		boolean b2;
		if (phone11==0) {
			b2 = true;
		} else {
			b2 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b2);
		resjson(response, map);
	}
	
	/**
	 * 查询顾客用户信息表
	 * 判断QQ是否重复
	 * @param phone
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_custom_userqq")
	public void findByIdBis_custom_userqq(Bis_custom_user qq,HttpServletRequest request,HttpServletResponse response) {
		String qq1=request.getParameter("qq");
		Integer qq11 = this.bcus.findByIdBis_custom_userqq(qq1);
		boolean b3;
		if (qq11==0) {
			b3 = true;
		} else {
			b3 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b3);
		resjson(response, map);
	}
	
	/**
	 * 查询顾客用户信息表
	 * 判断邮箱是否重复
	 * @param email
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_custom_useremail")
	public void findByIdBis_custom_useremail(Bis_custom_user email,HttpServletRequest request,HttpServletResponse response) {
		String email1=request.getParameter("email");
		Integer email11 = this.bcus.findByIdBis_custom_useremail(email1);
		boolean b4;
		if (email11==0) {
			b4 = true;
		} else {
			b4 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b4);
		resjson(response, map);
	}
	
}
